<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\common\service\message\ding;

use app\common\enum\setting\sms\Scene as SettingSmsScene;
use app\common\model\ding\BalanceLog as BalanceLogModel;
use app\common\model\store\Setting as SettingModel;
use app\common\model\store\Shop as ShopModel;
use app\common\service\message\Basics;
use think\facade\Db;

/**
 * 消息通知服务 [店员给用户预订成功]
 * Class Payment
 * @package app\common\service\message\order
 */
class Order extends Basics {
	/**
	 * 参数列表
	 * @var array
	 */
	protected $param = [
		'order' => [],
	];

	/**
	 * 订单页面链接
	 * @var array
	 */
	private $pageUrl = 'pages/order/detail';

	/**
	 * 发送消息通知
	 * @param array $param
	 * @return mixed|void
	 * @throws \app\common\exception\BaseException
	 * @throws \think\Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function send(array $param) {
		// 记录参数
		$this->param = $param;
		// 短信通知商家
		$this->onSendSms();
	}

	/**
	 * 短信通知商家
	 * @throws \think\Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	private function onSendSms(): void {

		$users = $this->param['users'];
		$clerk = $this->param['clerk'];

		$shopId = $clerk['shop_id'];
		$shop = $this->param['shop'];
		$balance = $shop['balance'];
		$shop_name = '[' . $shop['shop_name'] . ']';

		// 短信通知设置
		$smsConfig = SettingModel::getItem('sms', $this->storeId);
		$default = $smsConfig['default'];
		$fee = $smsConfig['engine'][$default]['fee']*3;
		if ($fee * count($users) > $balance) {
			throwError('短信发送失败：余额不足');
		}
		//消费记录
		$data['user_id'] = $clerk['user_id'];
		$data['shop_id'] = $shopId;
		$data['money'] = $fee * count($users);
		$data['scene'] = 50;
		$mobiles = array_column($users, 'mobile');

		$data['mobiles'] = json_encode($mobiles);
		$describeParam = count($mobiles);
		$scene = 50;
		BalanceLogModel::add($scene, $data, ['mobiles' => '1条']);

		//更新商家余额
		ShopModel::setDecBalance($shopId, $data['money']);
		foreach ($users as $key => $user) {
			$date = strtotime($user['date']);
			$date = date('m/d H:i', $date);

			// $params = [$shop_name, $user['real_name'], $user['seat_name'], $date, $clerk['real_name'], $clerk['mobile'], $shop['phone'], $user['page']];
			$params['store'] = $shop_name;
			$params['user'] = $user['real_name'];
			$params['table'] = $user['seat_name'];
			$params['time'] = $date;
			$params['manager'] = $clerk['real_name'];
			$params['mobilephone'] = (string)$clerk['mobile'];
			$params['telephone'] = (string)$shop['phone'];
			//$params['link'] = $user['page'];

			// 发送短信通知-预订成功
			$this->sendSms(SettingSmsScene::ORDER_YUDING, $user['mobile'], $params);
		}

	}

}